﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;

namespace Dynamic.Core.AD
{
    /// <summary>
    /// 定义默认参数
    /// 参数应当保存在配置文件中；如果没有设置，则应当手动设置。
    /// </summary>
    public class ADParaMgr
    {
        private ADParaMgr ( ) { }

        private static object s_lock = new object();
        private static volatile ADParaMgr s_value = null;

        private static ADParaMgr Value
        {
            get
            {
                if ( s_value == null )
                {
                    lock ( s_lock )
                    {
                        if ( s_value == null )
                        {
                            s_value = new ADParaMgr
                            {
                                _defaultUser = "defaultUser",
                                _defaultPWD = "pass@word1",
                                _defaultPath = "CN=Users"
                            };

                            NameValueCollection setting = ConfigurationManager.AppSettings;
                            // TODO:对以下设置（ADAdmin，ADPassword）进行加密，此时应当解密。
                            if ( !String.IsNullOrEmpty(setting["ADDomain"]) )
                                s_value._domain = setting["ADDomain"];
                            if ( !String.IsNullOrEmpty(setting["ADPath"]) )
                                s_value._fullPath = setting["ADPath"];
                            if ( !String.IsNullOrEmpty(setting["ADAdmin"]) )
                                s_value._userName = setting["ADAdmin"];
                            if ( !String.IsNullOrEmpty(setting["ADPassword"]) )
                                s_value._password = setting["ADPassword"];
                            if ( !String.IsNullOrEmpty(setting["ADDomain2000"]) )
                                s_value._domain2000 = setting["ADDomain2000"];
                            if ( !String.IsNullOrEmpty(setting["DefaultUser"]) )
                                s_value._defaultUser = setting["DefaultUser"];
                            if ( !String.IsNullOrEmpty(setting["DefaultPWD"]) )
                                s_value._defaultPWD = setting["DefaultPWD"];
                            if ( !String.IsNullOrEmpty(setting["DefaultPath"]) )
                                s_value._defaultPath = setting["DefaultPath"];
                        }
                    }
                }

                return s_value;
            }
        }

        /// <summary>
        /// 定义ADsPath前缀。
        /// </summary>
        public const string LDAP_IDENTITY = "LDAP://";

        private string _userName;        // AD 用户名，eg:administrator
        private string _domain;          // Domain Name，eg:contoso.com
        private string _domain2000;      // Domain Name 2000，eg:contoso
        private string _password;        // AD 用户密码
        private string _fullPath;        // Domain DN，eg:DC=contoso,DC=com，可以和contoso.com互相转化
        private string _defaultUser;     // 默认用户
        private string _defaultPWD;      // 默认用户密码
        private string _defaultPath;     // 默认位置,eg:CN=Users


        /// <summary>
        /// 设置系统参数
        /// </summary>
        /// <param name="userName">AD 用户名</param>
        /// <param name="domain">AD 域名称</param>
        /// <param name="password">AD 用户密码</param>
        /// <param name="fullPath">AD 域路径（DN）</param>
        public static void SetSysPara ( string userName, string domain, string password, string fullPath, string defaultUser, string defaultPWD, string defaultPath )
        {
            Value._userName = string.IsNullOrEmpty(userName) ? Value._userName : userName;
            Value._domain = string.IsNullOrEmpty(domain) ? Value._domain : domain;
            Value._password = string.IsNullOrEmpty(password) ? Value._password : password;
            Value._fullPath = string.IsNullOrEmpty(fullPath) ? Value._fullPath : fullPath;
            Value._defaultUser = string.IsNullOrEmpty(defaultUser) ? Value._defaultUser : defaultUser;
            Value._defaultPWD = string.IsNullOrEmpty(defaultPWD) ? Value._defaultPWD : defaultPWD;
            Value._defaultPath = string.IsNullOrEmpty(defaultPath) ? Value._defaultPath : defaultPath;

        }

        /// <summary>
        /// 获取AD 用户名
        /// </summary>
        public static string ADUserName
        {
            get { return Value._userName; }
        }

        /// <summary>
        /// 获取AD 域名称
        /// </summary>
        public static string ADDomain
        {
            get { return Value._domain; }
        }

        /// <summary>
        /// 获取AD 域名称2000
        /// </summary>
        public static string ADDomain2000
        {
            get { return Value._domain2000; }
        }

        /// <summary>
        /// 获取AD 用户密码
        /// </summary>
        public static string ADPassword
        {
            get { return Value._password; }
        }

        /// <summary>
        /// 获取AD 域路径（DN）
        /// </summary>
        public static string ADFullPath
        {
            get { return Value._fullPath; }
        }

        /// <summary>
        /// 获取AD 用户名
        /// </summary>
        public static string ADFullName
        {
            get { return String.Format("{0}@{1}", Value._userName, Value._domain); }
        }

        /// <summary>
        /// 获取默认用户
        /// 注：defaultUser（默认）,如有配置以配置文件(DefaultUser)为准
        /// </summary>
        public static string DefaultUser
        {
            get { return Value._defaultUser; }
        }

        /// <summary>
        /// 获取默认用户密码
        /// 注：pass@word1（默认）,如有配置以配置文件(DefaultPWD)为准
        /// </summary>
        public static string DefaultPWD
        {
            get { return Value._defaultPWD; }
        }

        /// <summary>
        /// 获取默认位置
        /// 注：CN=Users（默认）,如有配置以配置文件(DefaultPath)为准
        /// </summary>
        public static string DefaultPath
        {
            get { return Value._defaultPath; }
        }
    }
}
